package thebesttaskmanager.logical;

import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import thebesttaskmanager.inputparser.Command;
import thebesttaskmanager.inputparser.InputParser;
import thebesttaskmanager.tstore.TStore;
import thebesttaskmanager.idmanager.IdManager;

public class BestTaskManager {
  private static ArrayList<Task> mylist = new ArrayList<Task>();
  private static ArrayList<Task> myLastlist = new ArrayList<Task>();
  private static boolean flagOfExit = false;
  private static TStore myStore = new TStore("myfile.txt");
  private static InputParser myInputparser = new InputParser();
  private static boolean lastCommandIsDelete;
  private static int lastAssignedId;

  private static final String MESSAGE_WELCOME = "Welcone to BestTaskManager, please type your command";
  private static final String MESSAGE_ADD = "a new Task is added";
  private static final String MESSAGE_DELETE = "a task is delete";
  private static final String MESSAGE_EDIT = "the task is updated";
  private static final String MESSAGE_UNDO = "last command is cancled";
  private static final String MESSAGE_EXIT = "exit";

  public static void main(String[] args) {
    String input;
    String feedback;
    Command userCommand = new Command();
    Scanner myScanner = new Scanner(System.in);

    readFromFile();

    showToUser(MESSAGE_WELCOME);

    do {
      input = myScanner.nextLine();
      userCommand = myInputparser.getCommand(input);
      feedback = excuteCommand(userCommand);
      showToUser(feedback);
    } while (!flagOfExit);
    myScanner.close();
    flagOfExit = false;
    mylist.clear();

  }

  private static void readFromFile() {
    ArrayList<String> listInFile = new ArrayList<String>();
    listInFile = myStore.readFile();
    for (String s : listInFile) {
      mylist.add(new Task(myInputparser.getCommand(s)));
    }
  }

  private static ArrayList<Task> searchText(String words) {
    ArrayList<Task> resultlist = new ArrayList<Task>();
    for (Task e : mylist) {
      if (e.getText().contains(words)) {
	resultlist.add(e);
      }
    }
    return resultlist;
  }

  private static ArrayList<Task> SearchDate(Date startDate, Date endDate) {
    ArrayList<Task> resultlist = new ArrayList<Task>();
    for (Task e : mylist) {
      if ((e.getStartDate().after(startDate) || e.getStartDate().equals(startDate))
	  && (e.getEndDate().before(endDate) || e.getEndDate().equals(endDate))) {
	resultlist.add(e);
      }
    }
    return resultlist;
  }

  private static ArrayList<Task> searchDeadline(Date endDate) {
    ArrayList<Task> resultlist = new ArrayList<Task>();
    for (Task e : mylist) {
      if (e.getEndDate().before(endDate) || e.getEndDate().equals(endDate)) {
	resultlist.add(e);
      }
    }
    return resultlist;
  }

  private static ArrayList<Task> SearchType(String type) {
    ArrayList<Task> resultlist = new ArrayList<Task>();
    for (Task e : mylist) {
      if (e.getType().equals(type)) {
	resultlist.add(e);
      }
    }
    return resultlist;
  }

  private static Task searchID(String ID) {
    Task resultTask = new Task();
    for (Task e : mylist) {
      if (e.getID().equals(ID)) {
	resultTask = e;
      }
    }
    return resultTask;
  }

  public static void showToUser(String feedback) {
    System.out.println(feedback);
  }

  public static String add(Command cmd) {
    Task tempTask = new Task(cmd);

    myLastlist.clear();
    for (Task t : mylist) {
      myLastlist.add(t);
    }

    mylist.add(tempTask);
    lastCommandIsDelete = false;

    ArrayList<String> list = new ArrayList<String>();
    for (Task t : mylist) {
      list.add(t.toString());
    }
    myStore.writeFile(list);
    return MESSAGE_ADD;
  }

  public static String delete(Command cmd) {
    Task tempTask = new Task();
    tempTask = searchID(cmd.getText());

    IdManager.deleteId(Integer.parseInt(tempTask.getID()));
    lastCommandIsDelete = true;
    mylist.remove(tempTask);

    ArrayList<String> list = new ArrayList<String>();
    for (Task t : mylist) {
      list.add(t.toString());
    }
    myStore.writeFile(list);

    return MESSAGE_DELETE;
  }

  public static String edit(Command cmd) {

    return MESSAGE_EDIT;
  }

  public static String display(Command cmd) {
    return null;
  }

  // search
  public static String search(Command cmd) {
    return null;
  }

  public static String undo() {
    if (lastCommandIsDelete) {
      IdManager.addNewId(lastAssignedId);
    } else {// last command is add
      IdManager.deleteId(lastAssignedId);
    }
    mylist.clear();
    for (Task t : myLastlist) {
      mylist.add(t);
    }

    return MESSAGE_UNDO;
  }

  public static String excuteCommand(Command cmd) {
    if (cmd.getCmd().equalsIgnoreCase("add")) {
      return add(cmd);
    } else if (cmd.getCmd().equalsIgnoreCase("delete")) {
      return delete(cmd);
    } else if (cmd.getCmd().equalsIgnoreCase("edit")) {
      return edit(cmd);
    } else if (cmd.getCmd().equalsIgnoreCase("undo")) {
      return undo();
    } else if (cmd.getCmd().equalsIgnoreCase("display")) {
      return display(cmd);
    } else if (cmd.getCmd().equalsIgnoreCase("search")) {
      return search(cmd);
    } else if (cmd.getCmd().equalsIgnoreCase("exit")) {
      flagOfExit = true;
      return MESSAGE_EXIT;
    }
    return null;
  }

  public static void setLastAssignedId(Integer id) {
    lastAssignedId = id;
  }

}
